package com.lis.scf.controller;



//import com.enterprise.dto.request.CompanyInfoDTO;
//import com.enterprise.dto.request.CompanyQueryDTO;
//import com.enterprise.dto.response.CompanyDetailDTO;
//import com.enterprise.dto.response.PageResultDTO;
//import com.enterprise.dto.response.ResponseDTO;
//import com.enterprise.service.CompanyService;
import com.lis.scf.common.dto.PageResultDTO;
import com.lis.scf.common.dto.ResponseDTO;
import com.lis.scf.common.dto.request.CompanyInfoDTO;
import com.lis.scf.common.dto.request.CompanyQueryDTO;
import com.lis.scf.common.dto.response.CompanyDetailDTO;
import com.lis.scf.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

/**
 * 企业管理控制器
 */
@RestController
@RequestMapping("/api/companies")
@RequiredArgsConstructor
@Api(tags = "企业管理接口", description = "企业信息查询、完善、状态管理等功能")

public class CompanyController {

    private final CompanyService companyService;

    /**
     * 获取企业详情
     */
    @GetMapping("/{companyId}")
//    @PreAuthorize("hasAnyRole('企业管理员', '系统管理员')")
    @ApiOperation(
            value = "查询企业详情",
            notes = "根据企业ID查询企业基本信息和详细信息"
    )
    public ResponseDTO<CompanyDetailDTO> getCompanyDetail(
            @ApiParam(value = "企业ID", required = true)
            @PathVariable Long companyId) {

        CompanyDetailDTO detail = companyService.getCompanyDetail(companyId);
        return ResponseDTO.success( detail);
    }


    /**
     * 完善企业信息
     */
    @PutMapping("/{companyId}/info")
    @PreAuthorize("hasRole('企业管理员')")
    @ApiOperation(
            value = "完善企业信息",
            notes = "补充企业经营地址、经营范围、资产规模等详细信息"
    )
    public ResponseDTO<String> completeCompanyInfo(
            @ApiParam(value = "企业ID", required = true)
            @PathVariable Long companyId,
            @ApiParam(value = "企业信息参数", required = true)
            @Valid @RequestBody CompanyInfoDTO infoDTO) {
        companyService.completeCompanyInfo(companyId, infoDTO);
        return ResponseDTO.success("信息完善成功");
    }

    /**
     * 分页查询企业列表（管理员）
     */
    @GetMapping
//    @PreAuthorize("hasRole('系统管理员')")
    @ApiOperation(
            value = "查询企业列表",
            notes = "系统管理员分页查询所有企业，支持按名称和类型筛选"
    )
    public ResponseDTO<PageResultDTO<CompanyDetailDTO>> queryCompanyList(
            @Valid CompanyQueryDTO queryDTO) {
        PageResultDTO<CompanyDetailDTO> pageResult = companyService.queryCompanyList(queryDTO);
        return ResponseDTO.success( pageResult);
    }


    /**
     * 更新企业状态（激活/禁用）
     */
    @PutMapping("/{companyId}/status")
    @PreAuthorize("hasRole('系统管理员')")
    @ApiOperation(
            value = "更新企业状态",
            notes = "系统管理员启用或禁用企业（禁用后企业用户无法登录）"
    )
    public ResponseDTO<String> updateCompanyStatus(
            @ApiParam(value = "企业ID", required = true)
            @PathVariable Long companyId,
            @ApiParam(value = "是否激活（true=激活，false=禁用）", required = true)
            @RequestParam Boolean isActive) {
        companyService.updateCompanyStatus(companyId, isActive);
        return ResponseDTO.success(isActive ? "企业已激活" : "企业已禁用");
    }

    /**
     * 获取当前用户所属企业信息
     */
    @GetMapping("/current")
    @PreAuthorize("isAuthenticated()")
    @ApiOperation(
            value = "获取当前用户所属企业",
            notes = "登录用户查询自己所属的企业信息"
    )
    public ResponseDTO<CompanyDetailDTO> getCurrentCompany(Authentication authentication) {
        String username = authentication.getName();
        CompanyDetailDTO company = companyService.getCurrentUserCompany(username);
        return ResponseDTO.success( company);
    }
}
